﻿using Abp.Authorization;
using Abp.Localization;
using Abp.MultiTenancy;
using Mt.Site.Core.Authorization;
using System.Linq;

namespace Mt.Site.Core.Organizations
{
    public class PostPermissionsProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            var pages = context.GetPermissionOrNull(AppPermissions.Pages) ?? context.CreatePermission(AppPermissions.Pages, L("Pages"));
            var administration = pages.Children.FirstOrDefault(p => p.Name == AppPermissions.Pages_Administration)
                    ?? pages.CreateChildPermission(AppPermissions.Pages_Administration, L("Administration"));

            var organizationUnits = administration.Children.FirstOrDefault(p => p.Name == AppPermissions.Pages_Administration_OrganizationUnits)
                    ?? administration.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits, L("OrganizationUnits"));

            var manager = organizationUnits.CreateChildPermission(PostPermissions.Pages_Administration_OrganizationUnits_Post, L("Post"), multiTenancySides: MultiTenancySides.Tenant);
            manager.CreateChildPermission(PostPermissions.Pages_Administration_OrganizationUnits_Post_Create, L("CreatePost"), multiTenancySides: MultiTenancySides.Tenant);
            manager.CreateChildPermission(PostPermissions.Pages_Administration_OrganizationUnits_Post_Edit, L("EditPost"), multiTenancySides: MultiTenancySides.Tenant);
            manager.CreateChildPermission(PostPermissions.Pages_Administration_OrganizationUnits_Post_Delete, L("DeletePost"), multiTenancySides: MultiTenancySides.Tenant);
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, MtSiteConsts.LocalizationSourceName_Permissions);
        }
    }
}
